package com.yongninger.activity.slerkinfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yongninger.activity.R;
import com.yongninger.dao.SclerkDao;
import com.yongninger.vo.ClerkInfo;

public class ViewActivity extends Activity {
	private SclerkDao sclerkDao;
	ClerkInfo clerkInfo;
	EditText cname;
	EditText ccode;
	EditText ccompany;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clerk_view);
		sclerkDao = new SclerkDao(getApplicationContext());
		Intent intent = getIntent();
		clerkInfo = (ClerkInfo) intent
				.getSerializableExtra("clerkInfo");
		cname = (EditText)findViewById(R.id.cname);
		cname.setText(clerkInfo.getCname());
		ccode = (EditText)findViewById(R.id.ccode);
		ccode.setText(clerkInfo.getCcode());
		ccompany = (EditText)findViewById(R.id.ccompany);
		ccompany.setText(clerkInfo.getCcompany());

		Button modifyBtn = (Button) this.findViewById(R.id.modify);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (update()) {
					Toast.makeText(getApplicationContext(), "修改成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "修改失败", 2).show();
				}
			}
		};
		modifyBtn.setOnClickListener(onClickListener);

		/*
		 * 删除按钮
		 */
		Button deleteBtn = (Button) this.findViewById(R.id.delete);
		OnClickListener onClickListener1 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 删除
				boolean result = sclerkDao.deleteSclerkVo(clerkInfo.getCid());
				if (result) {
					Toast.makeText(getApplicationContext(), "删除成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "删除失败", 2).show();
				}
			}
		};
		deleteBtn.setOnClickListener(onClickListener1);
	}

	private boolean update() {
		ClerkInfo clerkInfoVoTemp = new ClerkInfo();
		clerkInfoVoTemp.setCcode(ccode.getText().toString());
		clerkInfoVoTemp.setCname(cname.getText().toString());
		clerkInfoVoTemp.setCcompany(ccompany.getText().toString());
		return sclerkDao.updateSclerkVo(clerkInfoVoTemp, clerkInfo.getCid());
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Toast.makeText(getApplicationContext(), "请先保存内容",
					Toast.LENGTH_SHORT).show();
			return false;
		}else if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() >= 0){
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ViewActivity.this,
					ListActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
